package com.cjkj.hospital.service.impl;

import com.cjkj.common.exception.ServiceException;
import com.cjkj.common.utils.DateUtils;
import com.cjkj.common.utils.SecurityUtils;
import com.cjkj.common.utils.uuid.IdUtils;
import com.cjkj.hospital.domain.THospitalUser;
import com.cjkj.hospital.domain.dto.HospitalUserReq;
import com.cjkj.hospital.domain.vo.HospitalUserVo;
import com.cjkj.hospital.mapper.THospitalUserMapper;
import com.cjkj.hospital.service.ITHospitalUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 医院用户Service业务层处理
 * 
 * @author cjkj
 * @date 2024-08-28
 */
@Service
public class THospitalUserServiceImpl implements ITHospitalUserService 
{
    @Resource
    private THospitalUserMapper tHospitalUserMapper;

    /**
     * 查询医院用户
     * 
     * @param userId 医院用户主键
     * @return 医院用户
     */
    @Override
    public THospitalUser selectTHospitalUserByUserId(String userId)
    {
        return tHospitalUserMapper.selectTHospitalUserByUserId(userId);
    }

    /**
     * 查询医院用户列表
     * 
     * @param req 医院用户
     * @return 医院用户
     */
    @Override
    public List<HospitalUserVo> selectTHospitalUserList(HospitalUserReq req)
    {
        return tHospitalUserMapper.selectTHospitalUserList(req);
    }

    /**
     * 新增医院用户
     * 
     * @param tHospitalUser 医院用户
     * @return 结果
     */
    @Override
    public int insertTHospitalUser(THospitalUser tHospitalUser)
    {
        String userName = tHospitalUser.getUserName();
        THospitalUser user = tHospitalUserMapper.selectByUserName(userName);
        if(user != null) {
            throw new ServiceException("用户账号：" + userName + "已存在！");
        }

        Date nowDate = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();

        tHospitalUser.setUserId(IdUtils.fastSimpleUUID());
        tHospitalUser.setPassword(SecurityUtils.encryptPassword(tHospitalUser.getPassword()));
        tHospitalUser.setCreateTime(nowDate);
        tHospitalUser.setCreateBy(username);
        tHospitalUser.setUpdateTime(nowDate);
        tHospitalUser.setUpdateBy(username);
        return tHospitalUserMapper.insertTHospitalUser(tHospitalUser);
    }

    /**
     * 修改医院用户
     * 
     * @param tHospitalUser 医院用户
     * @return 结果
     */
    @Override
    public int updateTHospitalUser(THospitalUser tHospitalUser)
    {
        String userName = tHospitalUser.getUserName();
        THospitalUser user = tHospitalUserMapper.selectByUserName(userName);
        if(user != null && !user.getUserId().equals(tHospitalUser.getUserId())) {
            throw new ServiceException("用户账号：" + userName + "已存在！");
        }

        tHospitalUser.setUpdateBy(SecurityUtils.getUsername());
        tHospitalUser.setUpdateTime(DateUtils.getNowDate());
        return tHospitalUserMapper.updateTHospitalUser(tHospitalUser);
    }

    /**
     * 批量删除医院用户
     * 
     * @param userIds 需要删除的医院用户主键
     * @return 结果
     */
    @Override
    public int deleteTHospitalUserByUserIds(String[] userIds)
    {
        // TODO 后续补充 判断是否有分配到该科室的设备
        // TODO 后续补充 删除用户前判断是否有未解绑的就诊数据

        return tHospitalUserMapper.deleteTHospitalUserByUserIds(userIds);
    }

    @Override
    public int resetPassword(THospitalUser hospitalUser) {
        hospitalUser.setPassword(SecurityUtils.encryptPassword(hospitalUser.getPassword()));
        hospitalUser.setUpdateBy(SecurityUtils.getUsername());
        hospitalUser.setUpdateTime(DateUtils.getNowDate());
        return tHospitalUserMapper.updateTHospitalUser(hospitalUser);
    }
}
